#include <liveMedia.hh>
#include <BasicUsageEnvironment.hh>
#include <GroupsockHelper.hh> // for "setUpOurSocket"

int main(int argc, char** argv) {
    // 创建使用环境
    TaskScheduler* scheduler = BasicTaskScheduler::createNew();
    UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);

    // 创建RTSP服务器
    RTSPServer* rtspServer = RTSPServer::createNew(*env, 8554);
    if (rtspServer == NULL) {
        *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
        exit(1);
    }

    // 设置服务器绑定地址
    int socket = setUpOurSocket(env, 8554);
    if (socket == -1) {
        *env << "Failed to set up server socket: " << env->getResultMsg() << "\n";
        exit(1);
    }
    rtspServer->setUpOurSocket(socket);

    // 创建流媒体会话
    ServerMediaSession* sms = ServerMediaSession::createNew(*env, "testStream", "testStream",
                                                            "Session streamed by \"testStream\"");
    sms->addSubsession(H264VideoFileServerMediaSubsession::createNew(*env, "test.mp4", False));
    rtspServer->addServerMediaSession(sms);

    char* url = rtspServer->rtspURL(sms);
    *env << "Play the stream using the URL: " << url << "\n";
    delete[] url;

    // 运行事件循环
    env->taskScheduler().doEventLoop();

    return 0;
}